package mlz.c_dead;

/*
    死锁程序
 */
public class DeadLockDemo {
    public static void main(String[] args) {
        Object objA = new Object();
        Object objB = new Object();

        new Thread(){
            @Override
            public void run() {
                //先进入A锁同步
                synchronized (objA){
                    System.out.println("线程1: 被锁住了");
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    //进入到同步锁B
                    synchronized (objB){
                        System.out.println("拿不到线程2");
                        try {
                            Thread.sleep(4000);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("被锁住了,无法打印~~");
                }
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                //先进入A锁同步
                synchronized (objB){
                    System.out.println("线程2: 被锁住了");
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    //进入到同步锁B
                    synchronized (objA){
                        System.out.println("拿不到线程1");
                        try {
                            Thread.sleep(4000);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("被锁住了,无法打印~~");
                }
            }
        }.start();


    }
}








